package view;



import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

import multithreaded.ControllerMultiThreaded;

import singlethreaded.ControllerSingleThreaded;

@SuppressWarnings("serial")
public class View extends JFrame implements ActionListener, PropertyChangeListener {

	
	private JComboBox callPutFlag;
	public JComboBox s;
	public JComboBox  x;
	public JComboBox  t;
	public JComboBox r;
	public JComboBox  b;
	public JComboBox v;
	public JTextField nSteps;
	public JTextField nSimulations;
	public JPanel top;
	public JPanel top2;
	public JPanel center;
	public JPanel bottom;
	public JButton launch;
	public JLabel info;
	public JProgressBar pb;
	public JLabel lab;
	public JCheckBox cb;
	
	/**
	 * Contructor
	 * @param parameters loaded from a file to be filled in several List
	 * @throws ParseException 
	 */
	
	public View(String[][] parameters) throws ParseException {
		
		//parameters for the window
		super();
		this.setTitle("Monitoring BIS");
		this.setSize(100,300);
		this.setResizable(true);
		this.setLocation(300,300);
		this.getContentPane().setLayout(new GridLayout(4,1));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
		
		// declare all the objects of the window
		this.callPutFlag = new JComboBox();
		this.s = new JComboBox();		
		this.x = new JComboBox();
		this.t = new JComboBox();
		this.r = new JComboBox();
		this.b =new JComboBox();
		this.v = new JComboBox();
		this.nSteps = new JTextField("168");
		this.nSimulations = new JTextField("10000");
		this.top = new JPanel();
		this.top2 = new JPanel();
		this.center = new JPanel();
		this.bottom = new JPanel();
		this.pb = new JProgressBar();
		this.pb.setStringPainted(true);
		this.lab = new JLabel();
		this.info = new JLabel();
		this.launch = new JButton("Launch");
		this.launch.addActionListener(this);
		this.cb = new JCheckBox("Multithread",true);
		
		// add the loaded values to their appropriates Jcombobox
		for(int i=0; i<5 ; i++)
		{
			this.callPutFlag.addItem(parameters[0][i]);
			this.s.addItem(parameters[1][i]);
			this.x.addItem(parameters[2][i]);
			this.t.addItem(parameters[3][i]);
			this.r.addItem(parameters[4][i]);
			this.b.addItem(parameters[5][i]);
			this.v.addItem(parameters[6][i]);
		}
	
		// pack these stuff correctly
		this.top.add(callPutFlag);
		this.top.add(s);
		this.top.add(x);
		this.top.add(t);
		this.top.add(r);
		this.top.add(b);
		this.top.add(v);
		this.top2.add(new JLabel("nSteps"));
		this.top2.add(nSteps);
		this.top2.add(new JLabel("nSimulations"));
		this.top2.add(nSimulations);
		
		this.center.add(cb);
		this.center.add(pb);
		this.center.add(launch);
		this.center.add(info);
		
		this.bottom.add(lab);
		
		this.getContentPane().add(this.top);
		this.getContentPane().add(this.top2);
		this.getContentPane().add(this.center);
		this.getContentPane().add(bottom);
		this.pack();
		//display
		this.setVisible(true);
	}
	
	/**
	 * Is performed when the user click on the launch button
	 */
	@Override
	public void actionPerformed(ActionEvent e) {	
		
		
		// if multithread is selected, we send the chosen values to compute the simulation
		if(!cb.isSelected())
		{
			ControllerSingleThreaded task = 
				new ControllerSingleThreaded(callPutFlag.getSelectedItem().toString(),
						Double.parseDouble(s.getSelectedItem().toString()),
						Double.parseDouble(x.getSelectedItem().toString()),
						Double.parseDouble(t.getSelectedItem().toString()), 
						Double.parseDouble(r.getSelectedItem().toString()),
						Double.parseDouble(b.getSelectedItem().toString()),
						Double.parseDouble(v.getSelectedItem().toString()),
						Integer.parseInt(nSteps.getText()),
						Integer.parseInt(nSimulations.getText()),
						this
						);
			// for progress bar rendering
			task.addPropertyChangeListener(this);
			task.execute();
		}
		//if singlethread is selected, we send the chosen values to compute the simulation
		else
		{
			ControllerMultiThreaded task = 
					new ControllerMultiThreaded(callPutFlag.getSelectedItem().toString(),
							Double.parseDouble(s.getSelectedItem().toString()),
							Double.parseDouble(x.getSelectedItem().toString()),
							Double.parseDouble(t.getSelectedItem().toString()), 
							Double.parseDouble(r.getSelectedItem().toString()),
							Double.parseDouble(b.getSelectedItem().toString()),
							Double.parseDouble(v.getSelectedItem().toString()),
							Integer.parseInt(nSteps.getText()),
							Integer.parseInt(nSimulations.getText()),
							this
							);
				// for progress bar rendering
				task.addPropertyChangeListener(this);
				task.execute();
		}
	}

	@Override
	// for progress bar rendering
	public void propertyChange(PropertyChangeEvent evt) {
		if ("progress" == evt.getPropertyName()) {
		      int progress = (Integer) evt.getNewValue();
		      pb.setValue(progress);
		    }
	}

}
